#include "pch.h"
#include <string>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <fstream>
using namespace std;

class singer
{
public:
	singer(int no, string name) :no(no), name(name) {}
	int no;
	string name;
	int score[10];
	int avgScore = 0;

	void inputScore()
	{
		for (int i = 0; i < 10; i++)
			cin >> score[i];
	}

	void caluAvg()
	{
		int max = 0;
		int min = 0;
		for (int i = 1; i < 10; i++)
		{
			if (score[i] > score[max])
				max = i;
			if (score[i] < score[min])
				min = i;
		}
		avgScore = 0;
		for (int i = 0; i < 10; i++)
		{
			if (i != max && i != min)
				avgScore += score[i];
		}
		avgScore /= 8;
	}

	void display()
	{
		cout << no << "号选手" << name << "平均分为" << avgScore << endl;
	}

	void toCsv(ofstream &out)
	{
		string content = to_string(no) + "," + name + "," + to_string(avgScore) + "\n";
		out << content.data();
	}
};


class singerManage
{
private:
	static bool cmp(singer &s1, singer &s2) { return s1.avgScore > s2.avgScore; }
public:
	static vector<singer> allSinger;
	static void caluSort()
	{
		for (singer &s : allSinger)
			s.caluAvg();

		sort(allSinger.begin(), allSinger.end(), cmp);
	}
};
vector<singer> singerManage::allSinger;


int main()
{
	while (1)
	{
		printf("1.添加选手数据\n");
		printf("2.评委打分\n");
		printf("3.成绩排序（按平均分）\n");
		printf("4.数据查询\n");
		printf("5.导出数据表\n");
		printf("6.退出系统\n");

		int f;
		cin >> f;
		switch (f)
		{
		case 1:
		{
			while (1)
			{
				int no;
				cout << "请输入选手编号：";	 cin >> no;
				string name;
				cout << "请输入选手姓名：";	 cin >> name;
				singerManage::allSinger.push_back(singer(no, name));
				string f;
				cout << "是否继续输入(Y/N)：";		cin >> f;
				if (f == "N")
					break;
			}
			break;
		}
		case 2:
		{
			for (singer &s : singerManage::allSinger)
			{
				cout << "请输入" << s.no << "号选手" << s.name << "的成绩：" << endl;
				s.inputScore();
			}
			break;
		}
		case 3:
		{
			singerManage::caluSort();
			for (singer &s : singerManage::allSinger)
				s.display();
			break;
		}
		case 4:
		{
			string name;
			cout << "请输入要查询的歌手姓名：";		cin >> name;
			for (singer &s : singerManage::allSinger)
			{
				if (s.name == name)
				{
					s.caluAvg();
					s.display();
				}
			}
			break;
		}
		case 5:
		{
			string path;
			cout << "请输入保存路径：";	cin >> path;
			ofstream out(path);
			if (out.is_open())
			{
				singerManage::caluSort();
				for (singer &s : singerManage::allSinger)
					s.toCsv(out);
				out.close();
			}
			else
				printf("打开失败\n");
			break;
		}
		case 6:
		{
			exit(0);
			break;
		}
		}
	}
}